import {SET_TODOLIST,SET_STATUS,DELETE_TODOLIST} from '../actions/todoActions'


let todoState = {
    todolist:[]
}

let TYPES ={
    [SET_TODOLIST](state,actions){
        state.todolist.push({
            id:new Date().getTime(),
            status:1,
            txt:actions.value
        })
    },
    [SET_STATUS](state,actions){
        // 找到当前需要被改变状态的元素
        let index = state.todolist.findIndex(item=>item.id === actions.value);

        // 设置元素状态值
        state.todolist[index].status = state.todolist[index].status === 1?2:1;
    },
    [DELETE_TODOLIST](state,actions){
         // 找到当前需要被改变状态的元素
         let index = state.todolist.findIndex(item=>item.id === actions.value);
         state.todolist.splice(index,1)
    }

}

let todo = (state = todoState,actions) =>{

    TYPES[actions.type]&&TYPES[actions.type](state,actions)
    
    return {...state,todolist:[...state.todolist]}    
}

export default todo